home *** CD-ROM | disk | FTP | other *** search
- /*
- ********************************************************************************
- **
- ** File: Preferences.cp
- **
- ** Authors: Cary Farrier (CF)
- **
- ** Description:
- **
- ** Prefs file routines.
- **
- ********************************************************************************
- **
- ** Revision History
- **
- ** 17-Oct-96 CF Created.
- ** 17-Nov-96 CF Converted to Apple Anaglyph.
- ** 23-May-97 gw Converted to Apple 3D SPEX.
- **
- ********************************************************************************
- */
- #include <Resources.h>
- #include <Folders.h>
- #include <Errors.h>
-
- #include "Preferences.h"
-
- /*
- ********************************************************************************
- ** static globals
- ********************************************************************************
- */
- static short gResFileRefnum, gOldResFileRefnum;
-
- /*
- ********************************************************************************
- ** local functions
- ********************************************************************************
- */
- static Handle OpenPreferences(void);
- static void ClosePreferences(void);
-
- /*
- ********************************************************************************
- **
- ** Name: ReadPreferences
- **
- ** Description:
- **
- ** Reads the prefs.
- **
- ********************************************************************************
- */
- OSStatus ReadPreferences(Preferences* outPrefs)
- {
- Handle thePrefsHandle;
-
- // get the preferences
- thePrefsHandle = OpenPreferences();
- if (thePrefsHandle)
- {
- *outPrefs = *((Preferences *) * thePrefsHandle);
- ClosePreferences();
- }
-
- return noErr;
- }
-
- /*
- ********************************************************************************
- **
- ** Name: WritePreferences
- **
- ** Description:
- **
- ** Writes the prefs.
- **
- ********************************************************************************
- */
- OSStatus WritePreferences(Preferences* outPrefs)
- {
- Handle thePrefsHandle;
-
- // get the preferences
- thePrefsHandle = OpenPreferences();
- if (thePrefsHandle)
- {
- *((Preferences *) * thePrefsHandle) = *outPrefs;
- ChangedResource(thePrefsHandle);
- ClosePreferences();
- }
-
- return noErr;
- }
-
- /*
- ********************************************************************************
- **
- ** Name: OpenPreferences
- **
- ** Description:
- **
- ** Opens the preferences file and returns a handle to the
- ** preferences data structure. If the prefs file doesn't exist, it is
- ** created. If the prefs data doesn't exist it is created.
- **
- ********************************************************************************
- */
- static Handle OpenPreferences(void)
- {
- short theVRefNum;
- long theDirID;
- OSStatus theError;
- FSSpec theSpec;
- Handle thePrefs;
-
- // save current res file
- gOldResFileRefnum = CurResFile();
-
- // locate the Preferences folder
- theError = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &theVRefNum, &theDirID);
- if (theError)
- return NULL;
-
- // create the spec for the file
- theError = FSMakeFSSpec(theVRefNum, theDirID, kPrefsName, &theSpec);
- if (theError)
- {
- // create the res file if it doesn't exist
- if (fnfErr == theError)
- {
- FSpCreateResFile(&theSpec, '????', kPrefsType, -1);
- if (ResError())
- return NULL;
- }
- else
- return NULL;
- }
-
- // open the resource file
- gResFileRefnum = FSpOpenResFile(&theSpec, fsRdWrPerm);
- if ((-1 == gResFileRefnum) || ResError())
- {
- return NULL;
- }
-
- // load the prefs resource
- thePrefs = GetResource(kPrefsType, kPrefsID);
- if (NULL == thePrefs)
- {
- // create it if it doesn't exist
- thePrefs = NewHandleClear(sizeof(Preferences));
- if (NULL == thePrefs)
- {
- CloseResFile(gResFileRefnum);
- UseResFile(gOldResFileRefnum);
- return NULL;
- }
-
- AddResource(thePrefs, kPrefsType, kPrefsID, kPrefsName);
- if (ResError())
- {
- CloseResFile(gResFileRefnum);
- UseResFile(gOldResFileRefnum);
- DisposeHandle(thePrefs);
- return NULL;
- }
- }
-
- // done
- return thePrefs;
- }
-
- /*
- ********************************************************************************
- **
- ** Name: ClosePreferences
- **
- ** Description:
- **
- ** Closes the preferences file.
- **
- ********************************************************************************
- */
- static void ClosePreferences(void)
- {
- CloseResFile(gResFileRefnum);
- UseResFile(gOldResFileRefnum);
- }
-
-
-